// 蜂窝蜜造平台生成代码，如手工更改，请添加到 .beeignore 忽略生成
package com.fowo.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fowo.api.common.model.IdListPo;
import com.fowo.api.common.model.OptionItem;
import com.fowo.api.common.model.R;
import com.fowo.api.common.util.ResponseUtils;
import com.fowo.api.entity.Warehouse;
import com.fowo.api.model.warehouse.WarehouseFBABinVo;
import com.fowo.api.model.warehouse.WarehouseItemVo;
import com.fowo.api.model.warehouse.WarehouseLocalAndOverseasVo;
import com.fowo.api.model.warehouse.WarehouseSearchParamPo;
import com.fowo.api.model.warehouse.WarehouseVo;
import com.fowo.api.service.WarehouseService;
import com.fowo.api.user.model.JwtUserInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StreamUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "仓库")
@RequestMapping("/api/warehouse")
@RestController
public class WarehouseController {

  @Resource
  private WarehouseService service;

  @Operation(summary = "查询结果")
  @GetMapping
  public R<List<WarehouseItemVo>> search(WarehouseSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.search(search));
  }

  @Operation(summary = "快速查询选项(有缓存)")
  @GetMapping("options")
  public R<List<OptionItem<WarehouseItemVo>>> searchOptions(
    WarehouseSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchOptions(search));
  }

  @Operation(summary = "查询最后更新时间")
  @GetMapping("lastUpdateTime")
  public R<Date> searchLastUpdateTime(WarehouseSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchLastUpdateTime(search));
  }

  @Operation(summary = "获取分页查询结果")
  @GetMapping("page")
  public R<Page<WarehouseItemVo>> getPage(WarehouseSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageSearch(search));
  }

  @Operation(summary = "查询是否后台下载")
  @GetMapping("getBackgroundExportFlag")
  public R<Boolean> getBackgroundExportFlag(WarehouseSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.getBackgroundExportFlag(search));
  }

  @Operation(summary = "获取详情")
  @GetMapping("{id}")
  public R<WarehouseVo> getVoById(@PathVariable Long id) {
    return R.data(() -> service.getVoById(id));
  }

  @Operation(summary = "获取仓库名称相关的编号列表")
  @GetMapping("name/{texts}/ids")
  public R<List<Long>> getIdsByName(@PathVariable String texts) {
    List<String> nameList = Arrays
      .stream(texts.split(","))
      .collect(Collectors.toList());
    return R.data(() -> service.getIdsByName(nameList));
  }

  @Operation(summary = "获取编号相关的仓库名称列表")
  @GetMapping("name/{ids}")
  public R<List<String>> getVoById(@PathVariable String ids) {
    List<Long> idList = Arrays
      .asList(ids.split(","))
      .stream()
      .map(Long::valueOf)
      .collect(Collectors.toList());
    return R.data(() -> service.getNameByIds(idList));
  }

  @PreAuthorize("@am.canAccess('warehouseCreate')")
  @Operation(summary = "新增")
  @PostMapping
  public R<Long> create(@Validated @RequestBody WarehouseVo model) {
    return R.data(() -> service.create(model));
  }

  @PreAuthorize("@am.canAccess('warehouseEdit')")
  @Operation(summary = "更新")
  @PutMapping
  public R update(@RequestBody WarehouseVo model) {
    return R.run(() -> service.update(model));
  }

  @PreAuthorize("@am.canAccess('warehouseEdit')")
  @Operation(summary = "编辑更新")
  @PutMapping("edit")
  public R updateForEdit(@RequestBody WarehouseVo model) {
    return R.run(() -> service.updateForEdit(model));
  }

  @PreAuthorize("@am.canAccess('warehouseDelete')")
  @Operation(summary = "删除")
  @DeleteMapping("{id}")
  public R delete(@PathVariable Long id) {
    return R.run(() -> service.delete(id));
  }

  @PreAuthorize("@am.canAccess('warehouseDelete')")
  @Operation(summary = "批量删除")
  @DeleteMapping("batch")
  public R batchDelete(@RequestBody IdListPo po) {
    return R.run(() -> service.batchDelete(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('warehouseAddBin')")
  @Operation(summary = "新增仓位")
  @PostMapping("addBin")
  public R addBin() {
    return R.run(() -> service.addBin());
  }

  @PreAuthorize("@am.canAccess('warehouseBatchAddBin')")
  @Operation(summary = "批量新增仓位")
  @PostMapping("batchAddBin")
  public R batchAddBin() {
    return R.run(() -> service.batchAddBin());
  }

  @PreAuthorize("@am.canAccess('warehouseBinExport1')")
  @Operation(summary = "仓位导出")
  @PostMapping("binExport1")
  public R binExport1() {
    return R.run(() -> service.binExport1());
  }

  @PreAuthorize("@am.canAccess('warehouseBinExportimport')")
  @Operation(summary = "仓位导入")
  @PostMapping("binExportimport")
  public R binExportimport() {
    return R.run(() -> service.binExportimport());
  }

  @PreAuthorize("@am.canAccess('warehouseBinandProductexport')")
  @Operation(summary = "仓位与商品关系导出")
  @PostMapping("binandProductexport")
  public R binandProductexport() {
    return R.run(() -> service.binandProductexport());
  }

  @PreAuthorize("@am.canAccess('warehouseBinimportProduct')")
  @Operation(summary = "仓位与商品关系导入")
  @PostMapping("binimportProduct")
  public R binimportProduct() {
    return R.run(() -> service.binimportProduct());
  }

  @PreAuthorize("@am.canAccess('warehouseSyncware')")
  @Operation(summary = "同步领星仓库数据")
  @PostMapping("syncware")
  public R syncware() {
    return R.run(() -> service.syncware());
  }

  @Operation(summary = "本地仓与海外仓")
  @GetMapping("local-and-overseas")
  public R<List<WarehouseLocalAndOverseasVo>> localAndOverseas(
    WarehouseSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.localAndOverseas(search));
  }

  @Operation(summary = "本地仓与海外仓分页")
  @GetMapping("local-and-overseasPage")
  public R<Page<WarehouseLocalAndOverseasVo>> localAndOverseasPage(
    WarehouseSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.localAndOverseasPage(search));
  }

  @Operation(summary = "本地仓与海外仓最后更新时间")
  @GetMapping("local-and-overseasLastUpdateTime")
  public R<Date> localAndOverseasLastUpdateTime(WarehouseSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.localAndOverseasLastUpdateTime(search));
  }

  @Operation(summary = "FBA仓")
  @GetMapping("f-b-a-bin")
  public R<List<WarehouseFBABinVo>> fBABin(WarehouseSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.fBABin(search));
  }

  @Operation(summary = "FBA仓分页")
  @GetMapping("f-b-a-binPage")
  public R<Page<WarehouseFBABinVo>> fBABinPage(WarehouseSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.fBABinPage(search));
  }

  @Operation(summary = "FBA仓最后更新时间")
  @GetMapping("f-b-a-binLastUpdateTime")
  public R<Date> fBABinLastUpdateTime(WarehouseSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.fBABinLastUpdateTime(search));
  }

  @Operation(summary = "选择用视图")
  @GetMapping("select-hide")
  public R<List<WarehouseItemVo>> selectHide(WarehouseSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.selectHide(search));
  }

  @Operation(summary = "选择用视图分页")
  @GetMapping("select-hidePage")
  public R<Page<WarehouseItemVo>> selectHidePage(
    WarehouseSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.selectHidePage(search));
  }

  @Operation(summary = "选择用视图最后更新时间")
  @GetMapping("select-hideLastUpdateTime")
  public R<Date> selectHideLastUpdateTime(WarehouseSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.selectHideLastUpdateTime(search));
  }
}
